package mn.slide.laborberlin.adaptor;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import mn.slide.laborberlin.adaptor.EinExtendableAdapter.ViewHolderChild;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.db.DatabaseManager;
import mn.slide.laborberlin.main.DetialActivity;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.main.UeberSchriftActivity;
import mn.slide.laborberlin.model.EmptyList;
import mn.slide.laborberlin.model.EntragData;
import mn.slide.laborberlin.model.Kapitel;
import mn.slide.laborberlin.model.SubUeberSchrift;
import mn.slide.laborberlin.model.UeberBase;
import mn.slide.laborberlin.model.UeberSchrift;
import mn.slide.laborberlin.model.UnterSuchung;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchListAdapter extends ArrayAdapter<EntragData>
{
	private final List<EntragData>	list;
	private final Activity			myContext;
	private LayoutInflater			inflate;

	public SearchListAdapter( Activity context , List<EntragData> list )
	{
		super(context , R.layout.list_item_middle , list);
		this.list = list;
		this.myContext = context;
		inflate = context.getLayoutInflater();
	}

	@Override
	public View getView( int position , View convertView , ViewGroup parent )
	{
		View rowView = convertView;
		final ViewHolderChild child;
		if (rowView == null)
		{
			rowView = inflate.inflate(R.layout.list_item_middle, null, true);
			child = new ViewHolderChild();
			child.name = (TextView) rowView.findViewById(R.id.title);
			child.detials = (TextView) rowView.findViewById(R.id.subtitle);
			child.arrow = (ImageView) rowView.findViewById(R.id.chevron);
			rowView.setTag(child);
		}
		else
		{
			child = (ViewHolderChild) rowView.getTag();
		}
		final EntragData data = list.get(position);
		String desc = null;
		if (data instanceof EmptyList)
		{
			child.detials.setVisibility(View.GONE);
			child.name.setText(data.getName());
		}
		else
		{
			if (data instanceof UnterSuchung)
			{
				final UnterSuchung unter = (UnterSuchung) data;
				rowView.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick( View v )
					{
						Intent intent = new Intent(myContext, DetialActivity.class);
						unter.setHistorySet(DatabaseManager.getInstance().getHistoryWithId());
						Log.v("History", "Added");
						Calendar today = Calendar.getInstance();
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String verlaufDate = format.format(today.getTime());
						unter.setVerlaufDate(verlaufDate);

						DatabaseManager.getInstance().updateUnter(unter);
						DataLayer.layer.addVerlList(unter);
						DataLayer.layer.setUnter(unter);
						HomeGroupActivity.group.goTo(intent);

					}
				});
				if (((UnterSuchung) data).getEntrag() != null)
				{
					Kapitel kapi = DataLayer.layer.getKapitelWithId(((UnterSuchung) data).getEntrag().getKapitel().getId());
					desc = kapi.getValue();

					child.detials.setText(Html.fromHtml(desc));
				}
				else if (((UnterSuchung) data).getUeberSchrift() != null)
				{
					UeberSchrift ueber = ((UnterSuchung) data).getUeberSchrift();
					desc = ueber.getName();
					Kapitel kapi = DataLayer.layer.getKapitelWithId(ueber.getEintrag().getKapitel().getId());
					desc = kapi.getValue() + " -> " + desc;

					child.detials.setText(Html.fromHtml(desc));
				}

				child.arrow.setVisibility(View.INVISIBLE);
			}
			else if (data instanceof UeberSchrift)
			{
				child.arrow.setVisibility(View.VISIBLE);
				rowView.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick( View v )
					{
						Intent intent = new Intent(myContext, UeberSchriftActivity.class);
						intent.putExtra("ueber", (UeberBase) data);
						HomeGroupActivity.group.goTo(intent);
					}
				});
				Kapitel kapi = DataLayer.layer.getKapitelWithId(((UeberSchrift) data).getEintrag().getKapitel().getId());
				if (desc != null)
				{
					desc = kapi.getValue() + " -> " + desc;
				}
				else
				{
					desc = kapi.getValue();
				}
				child.detials.setText(Html.fromHtml(desc));
				child.detials.setVisibility(View.VISIBLE);
			}
			else
			{
				desc = ((SubUeberSchrift) data).getUeberSchrift().getName();
				Kapitel kapi = DataLayer.layer.getKapitelWithId(((SubUeberSchrift) data).getUeberSchrift().getEintrag().getKapitel().getId());
				if (desc != null)
				{
					desc = kapi.getValue() + " -> " + desc;
				}
				else
				{
					desc = kapi.getValue();
				}
				child.detials.setText(Html.fromHtml(desc));
				child.detials.setVisibility(View.VISIBLE);
			}
			child.name.setText(Html.fromHtml(data.getName()));

		}

		return rowView;
	}
}
